function [RES,SS]=SteadyState_Solver_Res(PP,SS,Input)

%% Initiation
if nargin<3
    Input      =   [1];
end
FracHN_w    =   1;
DiscFactor  =   (1+SS.ir_ext)*(1+SS.dE)/(1+SS.Pir)-1;
FracHN_wN   =   Input(1);
% PSI         =   1.2;
% FracHN_w    =   1.1;
% DiscFactor  =   0.2;

SS.ExitFlag =   0;
%% Preliminaries
SS.BETA     =   1/(1+DiscFactor);
SS.FracHN_w =   FracHN_w;
SS.FracHN_wN=   FracHN_wN;
%--------------------------------------------------------------------------
% Directly Determined by the Steady State Target
%   Note:
%       1. 
%--------------------------------------------------------------------------
% Step 1. Solve mc_p_N, mc_p_H
% SS.mc_p_N   =   ( PP.EPSILON_N-1 )/PP.EPSILON_N + ...
%                 PP.THETA_N/PP.EPSILON_N*SS.Pir_N*(1+SS.Pir_N)*(SS.ir-SS.Pir_N)/(1+SS.ir);
% SS.mc_p_H   =   ( PP.EPSILON_H-1 )/PP.EPSILON_H + ...
%                 PP.THETA_H/PP.EPSILON_H*SS.Pir_H*(1+SS.Pir_H)*(SS.ir-SS.Pir_H)/(1+SS.ir);
SS.mc_p_N   =   ( PP.EPSILON_N-1 )/PP.EPSILON_N;
SS.mc_p_H   =   ( PP.EPSILON_H-1 )/PP.EPSILON_H;
% Step 2. Solve N_H, N_N, w_H, w_N
SS.N_N      =   SS.N*( SS.FracHN_w )/( SS.FracHN_w + SS.FracHN_wN );
SS.N_H      =   SS.N-SS.N_N;
SS.w_N      =   1/( 1+SS.FracHN_wN )/SS.N_N;
SS.w_H      =   SS.FracHN_wN/( 1+SS.FracHN_wN )/SS.N_H;
SS.w        =   SS.w_H;
% Step 3. Solve pY_N, pY_H
SS.pY_N     =   SS.w_N*SS.N_N/(1-PP.ALPHA_N)/SS.mc_p_N;
SS.pY_H     =   SS.w_H*SS.N_H/(1-PP.ALPHA_H)/SS.mc_p_H;

% SS.PAC_N    =   PP.THETA_N/2*SS.Pir_N^2*SS.pY_N;
% SS.PAC_H    =   PP.THETA_H/2*SS.Pir_H^2*SS.pY_H;
SS.PAC_N    =   0;
SS.PAC_H    =   0;
SS.Profit_N =   SS.pY_N-SS.PAC_N-SS.w_N*SS.N_N;
SS.Profit_H =   SS.pY_H-SS.PAC_H-SS.w_H*SS.N_H;
% Step 4. Solve Y, PAC, Profit
SS.Y        =   SS.pY_N+SS.pY_H;
SS.PAC      =   SS.PAC_N+SS.PAC_H;
SS.Profit   =   SS.Profit_N+SS.Profit_H;
% Step 5. Solve B and G
% SS.B        =   SS.Y*PP.B_Y_SS;
% SS.G        =   SS.B/(1+SS.ir)+SS.TAU-SS.B/(1+SS.Pir)-SS.T;

% SS.G        =   0;
% SS.B        =   (SS.G+SS.T-SS.TAU)*(1+SS.ir)*(1+SS.Pir)/(SS.Pir-SS.ir);

% SS.G        =   0;
% SS.B        =   0;
% SS.T        =   SS.TAU-SS.G-SS.B*(SS.ir-SS.Pir)/( (1+SS.Pir)*(1+SS.ir) );

% if SS.T+(SS.ir+PP.KAPPA)*PP.b_lb<0
%     warning('Transfer is too small to cover the unemployed consumption.');
% end

%% Solve the HH Problem
SS.B        =   PP.B_Total_SS;
SS.L        =   SS.N;
SS.C        =   SS.T+SS.Profit+(1-SS.TAU)*SS.w*SS.N+SS.B*(1+SS.dE)/(1+SS.Pir)-SS.B/(1+SS.ir_ext);
SS.PSI      =   (1-SS.TAU)*SS.w*( SS.C^(-PP.NU_C) )/( SS.L^(PP.NU_L) );

%% Aggregation
SS.pC_N     =   SS.FracN_pC*SS.C;
SS.pC_T     =   SS.FracT_pC*SS.C;
SS.pC_H     =   SS.FracH_pC*SS.C;
SS.pC_F     =   SS.FracF_pC*SS.C;
%% Residuals
RES_pC_N    =   SS.pC_N-SS.pY_N;

RES         =   [RES_pC_N];

RES_FinalGood=  SS.T+SS.Profit+SS.B*(1+SS.dE)/(1+SS.Pir)+(1-SS.TAU)*1 ...
                -SS.C-SS.B/(1+SS.ir_ext);

SS.RES      =   RES;
SS.RES_FinalGood ...
            =   RES_FinalGood;

%% Solve the Derived Quantities and Prices
% Prices
% SS.p_T      =   (SS.FracTN_pC/(1+SS.FracTN_pC)*1/PP.Cons_FracT)^(1/(1-PP.Cons_ElasTN));
% SS.p_N      =   (1/(1+SS.FracTN_pC)*1/(1-PP.Cons_FracT))^(1/(1-PP.Cons_ElasTN));
% SS.p_H      =   (SS.FracHF_pC/(1+SS.FracTN_pC)*1/PP.Cons_FracH)^(1/(1-PP.Cons_ElasHF))*SS.p_T;
% SS.p_F      =   (1/(1+SS.FracTN_pC)*1/(1-PP.Cons_FracH))^(1/(1-PP.Cons_ElasHF))*SS.p_T;
% Consumption Basket
SS.C_T      =   SS.pC_T/SS.p_T;
SS.C_N      =   SS.pC_N/SS.p_N;
SS.C_H      =   SS.pC_H/SS.p_H;
SS.C_F      =   SS.pC_F/SS.p_F;
% Production
SS.mc_N     =   SS.mc_p_N*SS.p_N;
SS.mc_H     =   SS.mc_p_H*SS.p_H;
SS.Y_N      =   SS.C_N;
SS.Y_H      =   SS.pY_H/SS.p_H;
SS.C_H_s    =   SS.Y_H-SS.C_H;
SS.Z_N      =   SS.Y_N/SS.N_N^(1-PP.ALPHA_N);
SS.Z_H      =   SS.Y_H/SS.N_H^(1-PP.ALPHA_H);
% Government
SS.GB       =   0;
SS.G        =   SS.GB/(1+SS.ir)+SS.TAU-SS.GB/(1+SS.Pir)-SS.T;